#include <semaphore.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  int flags, opt;
  mode_t perms;
  unsigned int value;
  sem_t *sem;
 
  flags = 0;
  perms = S_IRUSR | S_IWUSR;
  while ((opt = getopt(argc, argv, "cx"))!=-1) {
    switch (opt) {
    case 'c' :
      flags |= O_CREAT;
      break;
    case 'x':
      flags |= O_EXCL;
      break;
    default:
      printf("unknown option\n");
    }
  }
  if (optind >= argc)
    printf("wrong usage\n");
  value = (optind+1 < argc)?atoi(argv[optind+1]):0;
  sem = sem_open(argv[optind],flags,perms,value);
  if(sem == SEM_FAILED)
    printf("Failed to open the semaphore %s\n",argv[optind]);
  return 0;
}
